package com.cfuture.xiaozhi.smarthome.widget;

import com.cfuture.xiaozhi.smarthome.R;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class SmartHometWidgetProvider extends AppWidgetProvider {
	private final String WIDGET_ACTION = "com.cfuture.xiaozhi.smarthome.widget";
	/**
	 * 日光灯
	 */
	private final int WIDGET_BUTTON_TYPE_DAYLIGHT = 0;
	/**
	 * 白炽灯
	 */
	private final int WIDGET_BUTTON_TYPE_INCANDESCENT = 1;
	/**
	 * 空调
	 */
	private final int WIDGET_BUTTON_TYPE_SNOW = 2;
	/**
	 * 电视
	 */
	private final int WIDGET_BUTTON_TYPE_TV = 3;

	private static boolean daylightState = false;
	private static boolean incandescentState = false;
	private static boolean snowState = false;
	private static boolean tvState = false;

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		int buttonType = intent.getIntExtra("buttonType", -1);
		String a = intent.getAction();
		Log.v("xiaozhi", "onReceive:" + buttonType + "{" + a);
		// AppWidgetManager appWidgetManger = AppWidgetManager
		// .getInstance(context);
		// int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(
		// context, SmartHometWidgetProvider.class));
		// for (int id : appWidgetIds) {
		// Log.v("xiaozhi", id + ":id");
		// }
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		AppWidgetManager appWidgetManger = AppWidgetManager
				.getInstance(context);
		int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(
				context, SmartHometWidgetProvider.class));
		switch (buttonType) {
		case WIDGET_BUTTON_TYPE_DAYLIGHT:
			Log.i("xiaozhi", "toHeare");
			if (daylightState) {
				remoteView.setImageViewResource(R.id.widget_bt_daylight,
						R.drawable.widget_button_daylight_selector_off);
				Log.v("xiaozhi", "isOn=true");
				daylightState = false;
			} else {
				remoteView.setImageViewResource(R.id.widget_bt_daylight,
						R.drawable.widget_button_daylight_selector_on);
				Log.v("xiaozhi", "isOn=fasle");
				daylightState = true;
			}

			remoteView.setOnClickPendingIntent(
					R.id.widget_bt_daylight,
					getPenddingIntent(context, appWidgetManger, appIds,
							buttonType));
			appWidgetManger.updateAppWidget(appIds, remoteView);
			break;
		case WIDGET_BUTTON_TYPE_INCANDESCENT:
			if (incandescentState) {
				remoteView.setImageViewResource(R.id.widget_bt_incandescent,
						R.drawable.widget_button_incandescent_selector_off);
				Log.v("xiaozhi", "isOn=true");
				incandescentState = false;
			} else {
				remoteView.setImageViewResource(R.id.widget_bt_incandescent,
						R.drawable.widget_button_incandescent_selector_on);
				Log.v("xiaozhi", "isOn=fasle");
				incandescentState = true;
			}

			remoteView.setOnClickPendingIntent(
					R.id.widget_bt_incandescent,
					getPenddingIntent(context, appWidgetManger, appIds,
							buttonType));
			appWidgetManger.updateAppWidget(appIds, remoteView);
			break;
		case WIDGET_BUTTON_TYPE_SNOW:
			if (snowState) {
				remoteView.setImageViewResource(R.id.widget_bt_snow,
						R.drawable.widget_button_snow_selector_off);
				Log.v("xiaozhi", "isOn=true");
				snowState = false;
			} else {
				remoteView.setImageViewResource(R.id.widget_bt_snow,
						R.drawable.widget_button_snow_selector_on);
				Log.v("xiaozhi", "isOn=fasle");
				snowState = true;
			}

			remoteView.setOnClickPendingIntent(
					R.id.widget_bt_snow,
					getPenddingIntent(context, appWidgetManger, appIds,
							buttonType));
			appWidgetManger.updateAppWidget(appIds, remoteView);
			break;
		case WIDGET_BUTTON_TYPE_TV:
			if (tvState) {
				remoteView.setImageViewResource(R.id.widget_bt_tv,
						R.drawable.widget_button_tv_selector_off);
				Log.v("xiaozhi", "isOn=true");
				tvState = false;
			} else {
				remoteView.setImageViewResource(R.id.widget_bt_tv,
						R.drawable.widget_button_tv_selector_on);
				Log.v("xiaozhi", "isOn=fasle");
				tvState = true;
			}

			remoteView.setOnClickPendingIntent(
					R.id.widget_bt_tv,
					getPenddingIntent(context, appWidgetManger, appIds,
							buttonType));
			appWidgetManger.updateAppWidget(appIds, remoteView);
			break;
		default:
			break;
		}

	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		updateWidget(context, appWidgetManager, appWidgetIds);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		super.onDisabled(context);
	}

	/**
	 * 更新UI
	 * 
	 * @param context
	 * @param appWidgetManager
	 * @param appWidgetIds
	 */
	private void updateWidget(Context context,
			AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);

		remoteView.setOnClickPendingIntent(
				R.id.widget_bt_daylight,
				getPenddingIntent(context, appWidgetManager, appWidgetIds,
						WIDGET_BUTTON_TYPE_DAYLIGHT));
		// remoteView.setOnClickPendingIntent(R.id.widget_bt_daylight, pi);

		remoteView.setOnClickPendingIntent(
				R.id.widget_bt_incandescent,
				getPenddingIntent(context, appWidgetManager, appWidgetIds,
						WIDGET_BUTTON_TYPE_INCANDESCENT));

		remoteView.setOnClickPendingIntent(
				R.id.widget_bt_snow,
				getPenddingIntent(context, appWidgetManager, appWidgetIds,
						WIDGET_BUTTON_TYPE_SNOW));

		remoteView.setOnClickPendingIntent(
				R.id.widget_bt_tv,
				getPenddingIntent(context, appWidgetManager, appWidgetIds,
						WIDGET_BUTTON_TYPE_TV));
		appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
		Log.v("xiaozhi", "onUpdate");
	}

	private PendingIntent getPenddingIntent(Context context,
			AppWidgetManager appWidgetManager, int[] appWidgetIds,
			int buttonType) {
		for (int id : appWidgetIds) {
			Log.v("xiaozhi", id + ":id::" + buttonType);
		}
		Intent intent = new Intent();
		intent.setAction(WIDGET_ACTION + buttonType);
		intent.putExtra("buttonType", buttonType);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
		PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		return pi;
	}
}
